import QtQuick 1.1
import meegim 1.0

MyPage {
    id: vCardPage

    property string bareJid: ""
    property string nextPage: "qrc:/qml/RosterPage.qml"
    property bool readOnly: true

    property alias vCardPhoto: avatar.source
    property alias vCardNickName: tiNickname.text
    property alias vCardName: tiName.text
    property alias vCardMiddleName: tiMiddlename.text
    property alias vCardLastName: tiLastname.text
    property alias vCardFullName: tiFullName.text
    property alias vCardEmail: tiEmail.text
    property alias vCardBirthday: tiBirthday.text
    property alias vCardUrl: tiUrl.text

    Component.onCompleted: {
        showCurtain( true )
        if( main.requestMyVCard == true ) {
            console.log("QML::VCargPage: 1.Request vCard for: " + xmppClient.myBareJid )
            xmppClient.requestVCard( xmppClient.myBareJid )
            textCurtain.text = xmppClient.myBareJid
            toolBarButtonOptions.visible = true
            main.requestMyVCard = false
        } else {
            console.log("QML::VCargPage: 2.Request vCard for: " + xmppClient.chatJid )
            xmppClient.requestVCard( xmppClient.chatJid )
            textCurtain.text = xmppClient.chatJid
            toolBarButtonOptions.visible = false
        }
        clearForm()
    }

    Connections {
        target: xmppVCard
        onVCardChanged: {
            //console.log( "QML: VCardPage: onVCardChanged: " + xmppVCard.nickname )
            if( xmppVCard.photo != "" ) {
                vCardPhoto = xmppVCard.photo
            }
            vCardNickName = xmppVCard.nickname
            vCardName = xmppVCard.name
            vCardMiddleName = xmppVCard.middlename
            vCardLastName = xmppVCard.lastname
            vCardFullName = xmppVCard.fullname
            vCardEmail = xmppVCard.email
            vCardBirthday = xmppVCard.birthday
            vCardUrl = xmppVCard.url
            bareJid = xmppVCard.jid

            showCurtain( false )
        }
    }

    function showCurtain( isShow ) {
        if( isShow ) {
            curtain.opacity = 0.85
        } else {
            curtain.opacity = 0
        }
    }

    Rectangle {
        id: curtain
        z: 10
        anchors.top: parent.top
        anchors.bottom: toolBar.top
        anchors.left: parent.left
        anchors.right: parent.right
        color: "black"
        opacity: 0
        Behavior on opacity { NumberAnimation { duration: 400 } }
    }
    Column {
        z: curtain.z + 1
        anchors.centerIn: parent
        width: parent.width
        spacing: 5
        Image {
            id: imgVCardRequest
            source: "qrc:/qml/images/vcard.png"
            anchors.horizontalCenter: parent.horizontalCenter
            visible: curtain.opacity != 0 ? true : false
            opacity: curtain.opacity
        }
        Text {
            font.pixelSize: 26
            anchors.horizontalCenter: parent.horizontalCenter
            text: qsTr("Request vCard for")
            color: "white"
            visible: curtain.opacity != 0 ? true : false
            opacity: curtain.opacity
        }
        Text {
            id: textCurtain
            anchors.horizontalCenter: parent.horizontalCenter
            font.pixelSize: 26
            color: "white"
            visible: curtain.opacity != 0 ? true : false
            opacity: curtain.opacity
        }
    }

    function clearForm()
    {
        vCardPhoto = "qrc:/qml/images/avatar.png"
        vCardNickName = ""
        vCardName = ""
        vCardMiddleName = ""
        vCardLastName = ""
        vCardFullName = ""
        vCardEmail = ""
        vCardBirthday = ""
        vCardUrl = ""
        bareJid = ""
    }

    Flickable {
        id: flickArea
        anchors.top: parent.top; anchors.topMargin: 12
        anchors.bottom: toolBar.top; anchors.bottomMargin: 12
        anchors.left: parent.left; anchors.leftMargin: 20
        anchors.right: parent.right; anchors.rightMargin: 20

        contentHeight: columnContent.height
        contentWidth: columnContent.width

        flickableDirection: Flickable.VerticalFlick

        Column {
            id: columnContent
            width: vCardPage.width - flickArea.anchors.rightMargin - flickArea.anchors.leftMargin
            spacing: 10

            Row {
                id: rowAvatarAndJid
                height: avatar.height
                width: columnContent.width
                spacing: 15
                Image {
                    id: avatar
                    smooth: true
                    height: 80
                    width: height
                    source: "qrc:/qml/images/avatar.png"
                    anchors.verticalCenter: parent.verticalCenter
                }
                Text {
                    id: txtJid
                    text: bareJid
                    anchors.verticalCenter: parent.verticalCenter
                    font.pixelSize: 28
                }
            }


            Text {
                id: txtNickname
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Nickname:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiNickname
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }


            Text {
                id: txtName
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Name:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiName
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }


            Text {
                id: txtLastname
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Last name:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiLastname
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }


            Text {
                id: txtMiddlename
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Middle name:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiMiddlename
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }


            Text {
                id: txtFullName
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Full name:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiFullName
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }


            Text {
                id: txtEmail
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("E-mail:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiEmail
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }


            Text {
                id: txtBirthday
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Birthday:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiBirthday
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }


            Text {
                id: txtUrl
                anchors.horizontalCenter: parent.horizontalCenter
                text: qsTr("Url:")
                font.pixelSize: 28
            }
            MyTextInput {
                id: tiUrl
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width
                readOnly: vCardPage.readOnly
                __readonlyIsGray: false
            }
            Item { height: 10; width: vCardPage.width }

        } // Column

    }

    ToolBar {
        id: toolBar
        ToolButton {
            id: toolBarButtonDialog
            icon: "qrc:/qml/images/bar_roster.png"
            anchors.left: parent.left
            anchors.leftMargin: 30
            onClicked: {
                vCardPage.closePage( vCardPage.nextPage )
            }
            pauseAnim: 500
        }/*
        ToolButton {
            id: toolBarButtonOptions
            icon: "qrc:/qml/images/bar_ok.png"
            anchors.left: parent.left
            anchors.leftMargin: (3.5*(parent.width/4) - 0.5*toolBarButtonOptions.width)
            onClicked: {
                //TODO: Отправка vCard на сервер
                //xmppClient.setMyVCard()
                var objVCard = Qt.createQmlObject("import QtQuick 1.1; import meegim 1.0; XmppVCard {}", parent, "VCardPage.qml: XmppVCard" )
                objVCard.name = "xyxyxy"
                console.log(objVCard.name)
            }
            pauseAnim: 600
            visible: false
        }*/
    }

}
